package com.hky.springbootinit.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hky.springbootinit.common.ErrorCode;
import com.hky.springbootinit.constant.CommonConstant;
import com.hky.springbootinit.exception.BusinessException;
import com.hky.springbootinit.exception.ThrowUtils;
import com.hky.springbootinit.model.dto.notification.NotificationQueryRequest;
import com.hky.springbootinit.model.entity.Notification;
import com.hky.springbootinit.service.NotificationService;
import com.hky.springbootinit.mapper.NotificationMapper;
import com.hky.springbootinit.service.UserService;
import com.hky.springbootinit.utils.SqlUtils;
import com.hky.yunyuapicommon.model.vo.NotificationVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



/**
* @author HKY
* @description 针对表【notification】的数据库操作Service实现
* @createDate 2024-08-18 09:10:11
*/
@Service
@Slf4j
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification>
    implements NotificationService{



    @Override
    public void validNotification(Notification notification, List<String> domains, boolean add) {
        if(notification == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String title = notification.getTitle();
        String content = notification.getContent();
        Date startTime = notification.getStartTime();
        Date endTime = notification.getEndTime();

        if (ObjectUtil.isNull(startTime) || ObjectUtil.isNull(endTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "开始时间或结束时间为空");
        }

        // 校验域名是否已经存在公告
        if (!ObjectUtils.isEmpty(domains)) {
            LambdaQueryWrapper<Notification> queryWrapper = Wrappers.lambdaQuery(Notification.class);
            // 如果不是添加接口：排除当前的公告
            if (!add) {
                queryWrapper.ne(Notification::getId, notification.getId());
            }

            queryWrapper.and(wrapper -> domains.forEach(domain -> wrapper.or().like(Notification::getDomain, "\"" + domain + "\"")));
            long count = this.count(queryWrapper);
            log.info("repeat data count is: {}", count);
            ThrowUtils.throwIf(count > 0, ErrorCode.PARAMS_ERROR, "该域名已存在对应的公告");
        }

        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(title, content), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(title) && title.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题过长");
        }
        if (StringUtils.isNotBlank(content) && content.length() > 2048) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
        }
    }


    @Override
    public QueryWrapper<Notification> getQueryWrapper(NotificationQueryRequest notificationQueryRequest) {
        if (notificationQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");

        }
        // 获取请求中的数据
        Long id = notificationQueryRequest.getId();
        String title = notificationQueryRequest.getTitle();
        String content = notificationQueryRequest.getContent();
        Date startTime = notificationQueryRequest.getStartTime();
        Date endTime = notificationQueryRequest.getEndTime();
        Integer status = notificationQueryRequest.getStatus();
        List<String> domains = notificationQueryRequest.getDomain();
        String sortField = notificationQueryRequest.getSortField();
        String sortOrder = notificationQueryRequest.getSortOrder();

        // 拼接查询条件1656468492951523330_0.6480822755317348
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.ge(startTime != null, "startTime", startTime);
        queryWrapper.le(endTime != null, "endTime", endTime);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.eq("isDelete", false);
        if(CollectionUtils.isNotEmpty(domains)) {
            for (String domain : domains) {
                queryWrapper.like("domain", domain);
            }
        }
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public NotificationVO getNotificationVO(Notification notification) {
        if (notification == null) {
            return null;
        }
        NotificationVO notificationVO = new NotificationVO();
        BeanUtils.copyProperties(notification, notificationVO);
        return notificationVO;
    }

}




